package com.ermao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ermao.mapper.UserMapper;
import com.ermao.pojo.User;
import com.ermao.service.PunchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @author Ermao
 * Date: 2021/9/7 19:44
 */
@Service
public class PunchServiceImpl implements PunchService {
	private UserMapper userMapper;

	@Autowired
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Override
	public User getLastPunchDateAndDaysByUserId(Integer userId) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("days", "last_punch_date").eq("user_id", userId);
		return userMapper.selectOne(queryWrapper);
	}

	@Override
	public Boolean userPunch(Integer userId, LocalDate now, Integer days) {
		User user = new User();
		user.setUserId(userId);
		user.setLastPunchDate(now);
		user.setDays(days + 1);
		return userMapper.updateById(user) > 0;
	}

	@Override
	public Integer selectUserPunchDays(Integer userId) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("days").eq("user_id", userId);
		User user = userMapper.selectOne(queryWrapper);
		if (user == null) {
			return null;
		} else {
			return user.getDays();
		}
	}
}
